<?php
$time = time();


/*
*function：计算两个日期相隔多少年，多少月，多少天
*param string $date1[格式如：2011-11-5]
*param string $date2[格式如：2012-12-01]
*return array array('年','月','日');
*/
function diffDate($date1,$date2){
	if(strtotime($date1)>strtotime($date2)){
		$tmp=$date2;
		$date2=$date1;
		$date1=$tmp;
	}
	list($Y1,$m1,$d1)=explode('-',$date1);
	list($Y2,$m2,$d2)=explode('-',$date2);
	$Y=$Y2-$Y1;
	$m=$m2-$m1;
	$d=$d2-$d1;
	if($d<0){
		$d+=(int)date('t',strtotime("-1 month $date2"));
		$m--;
	}
	if($m<0){
		$m+=12;
		$y--;
	}
	return array('year'=>$Y,'month'=>$m,'day'=>$d);
}


function isnumber($str){
  return is_numeric($str)?$str:0;
}

function ajax_url($url,$parame='set_parameter'){
	preg_match_all('#.*?href="([^"]*)"[^>]*>#i', $url, $match);

	

	foreach($match[1] as $imgurl){
		$url1=str_replace('?&page=',"",$imgurl);	
	
		$url=str_replace($imgurl.'"',"javascript:{$parame}('page','{$url1}')".'"',$url);
	}
	return $url;
}

//去除html
function DeleteHtml($str){ 
	$str = trim($str); //清除字符串两边的空格
	$str = strip_tags($str,""); //利用php自带的函数清除html格式
	/*$str = preg_replace("/\t/","",$str); //使用正则表达式替换内容，如：空格，换行，并将替换为空。
	$str = preg_replace("/\r\n/","",$str); 
	$str = preg_replace("/\r/","",$str); 
	$str = preg_replace("/\n/","",$str); 
	$str = preg_replace("/ /","",$str);
	$str = preg_replace("/? /","",$str);  //匹配html中的空格*/
	$str = str_replace("&nbsp;","",$str); 
	return trim($str); //返回字符串
}

//计算2个日期天数
function diffBetweenTwoDays ($day1, $day2)
{
  $second1 = strtotime($day1);
  $second2 = strtotime($day2);
    
  if ($second1 < $second2) {
    $tmp = $second2;
    $second2 = $second1;
    $second1 = $tmp;
  }
  return ($second1 - $second2) / 86400;
}



//判断admin 权限
function is_admin($id){
	global $db,$pre,$adminarr;
	$rs2 = $db->get_one("select u_id from {$pre}admin where u_id='{$adminarr['u_id']}' and  concat(',',concat(u_ssion,',')) like '%,{$id},%'");
	if($rs2){
		return true;
	}else{
		return false;
	}
}



 function createNonceStr($length = 16) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $str = "";
    for ($i = 0; $i < $length; $i++) {
      $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    return $str;
  }	



function admin_group($id){
	global $db,$pre;
	$query = $db->get_one("select title from {$pre}admin_group where id='$id' ");
	return $query[title];
}


function get_category($id) { //栏目信息
	global $db,$pre,$string,$webdb;
	if ($id) { 
		$query = $db->query("select * from {$pre}menu where m_id='$id'");
		//$string="<a href='/'>首页</a>";
		if($db->num_rows($query)>0)	{
			$rs = $db->fetch_array($query);
			$string="<span class=\"c-gray en\">&gt;</span> {$rs['m_title']}".$string; // & gt; 中间空格去掉为实体字符>> 
			get_category($rs['m_fid']); 
		}
		return "<i class=\"icon-home\"></i><a href='welcome.php'>首页</a> ".$string; 
	}else { 
		return false; 
	} 
}

function department_category($id) { //栏目信息
	global $db,$pre,$string1,$webdb;
	if ($id) { 
		$query = $db->query("select * from {$pre}department where id='$id'");
		//$string="<a href='/'>首页</a>";
		if($db->num_rows($query)>0)	{
			$rs = $db->fetch_array($query);
			$string1=$string1?"{$rs['name']}-".$string1:$rs['name']; // & gt; 中间空格去掉为实体字符>> 
			if($rs['parentid']!='1'){
				department_category($rs['parentid']); 
			}
		}
		return $string1; 
	}else { 
		return false; 
	} 
}

function department_top($id) { //栏目信息
	global $db,$pre,$string2,$webdb;
	if ($id) { 
		$query = $db->query("select * from {$pre}department where id='$id'");
		//$string="<a href='/'>首页</a>";
		if($db->num_rows($query)>0)	{
			$rs = $db->fetch_array($query);
			$string2=$rs['id']; // & gt; 中间空格去掉为实体字符>> 
			if($rs['parentid']!='1'){
				department_top($rs['parentid']); 
			}
		}
		return $string2; 
	}else { 
		return false; 
	} 
}



function logo($title,$uname='',$content='',$type=1,$uid=''){
	global $onlineip,$db,$pre,$webdb,$admin_id,$timestamp;
	$uname=$uname?$uname:$_SESSION[$webdb['session_admin']];
	if($type==1){
		//$admin_id
	}else{
		if($uid!=''){
			$admin_id=$uid;
		}else{
			$rsuser=$db->get_one("select id from {$pre}user where userid='$uname'");
			$admin_id=$rsuser['id'];
		}
	}
	if(!$content){
		foreach($_REQUEST as $key=>$rs){
			if(is_array($rs)){
				unset($dds);
				$dds="array<br>(";
				foreach($rs as $kk=>$rr){
					$dds.=$kk.'=>'.$rr.'<br>';
				}
				$rs=$dds.")";
			}
			$content1.=$key.'=>'.$rs.'<br>';
		}
		$content=$content1;
	}else{
		if(is_array($content)){			
			$dds="";
			foreach($content as $kk=>$rr){
				$dds.=$kk.'=>'.$rr.'<br>';
			}
			$content=$dds;
		}
	}
    $admin_id=$admin_id?$admin_id:0;
    $content=filtrate($content);
    $title=filtrate($title);
	$query = $db->query("INSERT INTO {$pre}log (`admin`,`time`,`content`,`title`,`ip`,`adminid`,`type`) VALUES ('$uname','$timestamp','$content','$title','$onlineip','$admin_id','$type')");
}



function user_showerr($msg){
	die("<script>window.parent.alert('$msg');</script>");
	exit;
}
function user_refreshto($url,$msg,$t='0'){
	if($t>0){
		
		echo "<script>
		window.parent.alert('$msg');		
		window.parent.location='$url';
		</script>";
	}else{
		echo "<script>
		window.parent.location='$url';
		</script>";
	}
	
}



/**
*读文件函数
**/
function read_file($filename,$method="rb"){
	if($handle=@fopen($filename,$method)){
		@flock($handle,LOCK_SH);
		$filedata=@fread($handle,@filesize($filename));
		@fclose($handle);
	}
	return $filedata;
}


function dianpu($id){
	global $db,$pre;
	$dbrs=$db->get_one("select name from {$pre}department where id='{$id}'");
	$na=$dbrs['name']?$dbrs['name']:'-';
	return $na; 
}


function admin_name($id){
	global $db,$pre;
	$dbrs=$db->get_one("select u_name from {$pre}admin where u_id='$id'");
	$na=$dbrs['u_name']?$dbrs['u_name']:'-';
	return $na; 
}

function admin_ids($name){
	global $db,$pre;
	$dbrs=$db->get_one("select u_id from {$pre}admin where u_name='$name'");
	$na=$dbrs['u_id']?$dbrs['u_id']:'0';
	return $na; 
}

/**
*utf8->gb2312
**/
function utf_gb2312($str){
	$str=iconv("UTF-8","gb2312//IGNORE",$str);
	return $str;
}


/**
*写文件函数
**/
function write_file($filename,$data,$method="rb+",$iflock=1){
	@touch($filename);
	$handle=@fopen($filename,$method);
	if($iflock){
		@flock($handle,LOCK_EX);
	}
	@fputs($handle,$data);
	if($method=="rb+") @ftruncate($handle,strlen($data));
	@fclose($handle);
	@chmod($filename,0777);	
	if( is_writable($filename) ){
		return 1;
	}else{
		return 0;
	}
}
/**
*截取字符
**/
function get_word($content,$length,$more=1) {
	if(!$more){
		$length=$length+2;
	}
	if($length>10){
		$length=$length-2;
	}
	if($length && strlen($content)>$length){
		$num=0;
		for($i=0;$i<$length-1;$i++) {
			if(ord($content[$i])>127){
				$num++;
			}
		}
		$num%2==1 ? $content=substr($content,0,$length-2):$content=substr($content,0,$length-1);
		$more && $content.='..';
	}
	return $content;
}


function get_utf8_word($string, $length = 80,$more=1 , $etc = '..')
{
	$strcut = '';
	$strLength = 0;
	$width  = 0;
	if(strlen($string) > $length) {
		//将$length换算成实际UTF8格式编码下字符串的长度
		for($i = 0; $i < $length; $i++) {
			if ( $strLength >= strlen($string) ){
				break;
			}
			if ( $width>=$length){
				break;
			}
			//当检测到一个中文字符时
			if( ord($string[$strLength]) > 127 ){
				$strLength += 3;
				$width     += 2;              //大概按一个汉字宽度相当于两个英文字符的宽度
			}else{
				$strLength += 1;
				$width     += 1;
			}
		}
		return substr($string, 0, $strLength).$etc;
	} else {
		return $string;
	}
}


function cth($msg){
	$msg=trim($msg);
	$msg = str_replace('"','&quot;',$msg);
	$msg = str_replace("'",'&#39;',$msg);
	$msg = str_replace("<","&lt;",$msg);
	$msg = str_replace(">","&gt;",$msg);
	$msg = str_replace("\t","   &nbsp;  &nbsp;",$msg);
	$msg = str_replace("\r","",$msg);
	$msg = str_replace(chr(10),"",$msg);
	$msg = str_replace(chr(13),"",$msg);
	$msg = str_replace(chr(13).chr(10),"",$msg);
	$msg = str_replace("   ","",$msg);
	return $msg;
}

/**
*过滤安全字符
**/
function filtrate($msg){
	//
	//$msg = str_replace(' ','&nbsp;',$msg);
	
	$msg = str_replace("#","&#35;",$msg);
	$msg = str_replace('&','&#38;',$msg);
	
	$msg = str_replace('"','&quot;',$msg);
	$msg = str_replace("'",'&#39;',$msg);
	$msg = str_replace("<","&lt;",$msg);
	$msg = str_replace(">","&gt;",$msg);
	$msg = str_replace(">","&gt;",$msg);
	
	$msg = str_replace("(","&#40;",$msg);
	$msg = str_replace(")","&#41;",$msg);
	
	
	$msg = str_replace("\t","   &nbsp;  &nbsp;",$msg);
	//$msg = str_replace("\r","",$msg);
	$msg = str_replace("   "," &nbsp; ",$msg);
	return $msg;
}


/**
*分页
**/

function wap_getpage($table,$choose,$url,$rows=20,$total=''){
	global $page,$db;
	if(!$page){
		$page=1;
	}
	//当存在$total的时候.就不用再读数据库
	if(!$total && $table){
		$query=$db->get_one("SELECT COUNT(*) AS num  FROM $table $choose");
		$total=$query['num'];
	}
	$totalpage=@ceil($total/$rows);
	$nextpage=$page+1;
	$uppage=$page-1;
	if($nextpage>$totalpage){
		$nextpage=$totalpage;
	}
	if($uppage<1){
		$uppage=1;
	}
	$s=$page-3;
	if($s<1){
		$s=1;
	}
	$b=$s;
	for($ii=0;$ii<6;$ii++){
		$b++;
	}
	if($b>$totalpage){
		$b=$totalpage;
	}
	for($j=$s;$j<=$b;$j++){
		if($j==$page){
			$show.=" <a href='#'><font color=red>$j</font></a>";
		}else{
			$show.=" <a href=\"$url&page=$j\" title=\"第{$j}页\">$j</a>";
		}
	}
	
	
	
	$showpage="
	<a href=\"$url&page=1\" class='weui_btn weui_btn_mini weui_btn_plain_primary'>首页</a>
	<a href=\"$url&page=$uppage\" class='weui_btn weui_btn_mini weui_btn_plain_primary'>上一页</a>
	<a href=\"$url&page=$nextpage\" class='weui_btn weui_btn_mini weui_btn_plain_primary' >下一页</a>
	<a href=\"$url&page=$totalpage\" class='weui_btn weui_btn_mini weui_btn_plain_primary'>尾页</a>
	<a class='weui_btn weui_btn_mini weui_btn_plain_primary'>".$page."/".$totalpage."</a>
	";
    
	
	
	
	if($totalpage>1){
		return $showpage;
	}else{
		
		return "<div align=center style='font-size:12px;color:#ccc'>没有更多信息了^^</div>";
		
		}
}

function wap_refreshto($url,$msg,$time=1){
	if($time==0){
		header("location:$url");
	}else{
		ob_end_clean();
		ob_start();
		require(CHEN."template/default/wap_refreshto.htm");
	}
	exit;
}


function wap_showerr($msg,$type=''){
	if($type==1){
		$showerrMsg=str_replace("'","\'",$msg);
		echo "<SCRIPT LANGUAGE=\"JavaScript\">
		<!--
		alert('$showerrMsg');
		if(document.referrer==''&&window.self==window.top){
			window.self.close();
		}else{
			history.back(-1);
		}		
		//-->
		</SCRIPT>";
	}else{
		extract($GLOBALS);
		require(CHEN."template/default/wap_showerr.htm");
	}
	exit;
}


function getpage($table,$choose,$url,$rows=20,$total=''){
	global $page,$db;
	if(!$page){
		$page=1;
	}
	//当存在$total的时候.就不用再读数据库
	if(!$total && $table){
		$query=$db->get_one("SELECT COUNT(*) AS num  FROM $table $choose");
		$total=$query['num'];
	}
	$totalpage=@ceil($total/$rows);
	$nextpage=$page+1;
	$uppage=$page-1;
	if($nextpage>$totalpage){
		$nextpage=$totalpage;
	}
	if($uppage<1){
		$uppage=1;
	}
	$s=$page-3;
	if($s<1){
		$s=1;
	}
	$b=$s;
	for($ii=0;$ii<6;$ii++){
		$b++;
	}
	if($b>$totalpage){
		$b=$totalpage;
	}
	for($j=$s;$j<=$b;$j++){
		if($j==$page){
			$show.=" <a href='#'><font color=red>$j</font></a>";
		}else{
			$show.=" <a href=\"$url&page=$j\" title=\"第{$j}页\">$j</a>";
		}
	}
	$showpage="<a href=\"$url&page=1\" title=\"首页\">首页</A> <a href=\"$url&page=$uppage\" title=\"上一页\">上一页</A>  {$show}  <a href=\"$url&page=$nextpage\" title=\"下一页\">下一页</A> <a href=\"$url&page=$totalpage\" title=\"尾页\">尾页</A> <a href='#'><font color=red>$page</font>/$totalpage/$total</a>";
    if($totalpage>1){
		return $showpage;
	}
}

///编码转换
function unescape($str){
	//$str="&#20998&#20844&#21496";
	//$str = "TTL&#20840;&#22825;&#20505;&#33258;&#21160;&#32858;&#28966;";
	$str = preg_replace("|&#([0-9]{1,5})|", "\".u2utf82gb(\\1).\"", $str);
	$str = "\$str=\"$str\";";
	eval($str);
	return $str;
}

function u2utf82gb($c){
    $str="";
    if ($c < 0x80) {
         $str.=$c;
    } else if ($c < 0x800) {
         $str.=chr(0xC0 | $c>>6);
         $str.=chr(0x80 | $c & 0x3F);
    } else if ($c < 0x10000) {
         $str.=chr(0xE0 | $c>>12);
         $str.=chr(0x80 | $c>>6 & 0x3F);
         $str.=chr(0x80 | $c & 0x3F);
    } else if ($c < 0x200000) {
         $str.=chr(0xF0 | $c>>18);
         $str.=chr(0x80 | $c>>12 & 0x3F);
         $str.=chr(0x80 | $c>>6 & 0x3F);
         $str.=chr(0x80 | $c & 0x3F);
    }
    return iconv('UTF-8', 'GB2312', $str);
}

/**
*页面跳转函数
**/
function refreshto($url,$msg,$time=1){
	if($time==0){
		header("location:$url");
	}else{
		ob_end_clean();
		ob_start();
		require(CHEN."template/default/refreshto.htm");
	}
	exit;
}
function ert($str){
	echo "<SCRIPT LANGUAGE=\"JavaScript\">
		<!--
		alert('$str');
		//-->
		</SCRIPT>";
		exit;
}

/**
*警告页面函数
**/
function showerr($msg,$type=''){
	if($type==1){
		$showerrMsg=str_replace("'","\'",$msg);
		echo "<SCRIPT LANGUAGE=\"JavaScript\">
		<!--
		alert('$showerrMsg');
		if(document.referrer==''&&window.self==window.top){
			window.self.close();
		}else{
			history.back(-1);
		}		
		//-->
		</SCRIPT>";
	}else{
		extract($GLOBALS);
		require(CHEN."template/default/showerr.htm");
	}
	exit;
}

 
/**
*取得随机字符
**/
function rands($length,$strtolower=1) {
	$hash = '';
	$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
	$max = strlen($chars) - 1;
	mt_srand((double)microtime() * 1000000);
	for($i = 0; $i < $length; $i++) {
		$hash .= $chars[mt_rand(0, $max)];
	}
	if($strtolower==1){
		$hash=strtolower($hash);
	}
	return $hash;
}

/**
*简体中文转UTF8编码
**/
/*function gbk2utf8($text) {
	$fp = fopen(CHEN."inc/gbkcode/gbk2utf8.table","r");
	while(! feof($fp)) {
		list($gb,$utf8) = fgetcsv($fp,10);
		$charset[$gb] = $utf8;
	}
	fclose($fp);		//以上读取对照表到数组备用wl__hd_sg2_02.gif

	//提取文本中的成分，汉字为一个元素，连续的非汉字为一个元素
	preg_match_all("/(?:[\x80-\xff].)|[\x01-\x7f]+/",$text,$tmp);
	$tmp = $tmp[0];
	//分离出汉字
	$ar = array_intersect($tmp, array_keys($charset));
	//替换汉字编码
	foreach($ar as $k=>$v)
    $tmp[$k] = $charset[$v];
	//返回换码后的串
	return join('',$tmp);
}
*/
function gbk2utf8($str){
	$str=iconv("gb2312","UTF-8",$str);
	return $str;
}

function utf8gbk2($str){
	//$str=iconv("UTF-8","gb2312",$str);
	
	$str=mb_convert_encoding($str, "GBK", "UTF-8");
	
	return $str;
}

/**
*记录cookie
**/

function set_cookie($name,$value,$cktime=0){
	global $webdb;
	if($cktime!=0){
		$cktime=time()+$cktime;
	}
	if($value==''){
		$cktime=time()-31536000;
	}
	$S = $_SERVER['SERVER_PORT'] == '443' ? 1:0;
	if($webdb[cookiePath]){
		$path=$webdb[cookiePath];
	}else{
		$path="/";
	}
	$domain=$webdb[cookieDomain];
	setcookie("$webdb[cookiePre]$name",$value,$cktime,$path,$domain,$S);
}

/**
*获取cookie
**/

function get_cookie($name){
	global $webdb,$_COOKIE,$_SESSION;
	//return $_SESSION[$name];
    return $_COOKIE["$webdb[cookiePre]$name"];
}



//获取UNIX时间,主要是特别处理变成整数.不加引号08与8会不一样的结果,加引号是正常的
function mk_time($h,$i, $s, $m, $d, $y){
	$time=@mktime(intval($h),intval($i),intval($s),intval($m),intval($d),intval($y));
	return $time;
}


//检测某个关键字是否包含在数组里边
function ifin_array($array,$filename,$ISin=''){
	foreach($array as $key=>$value){
		if(!is_array($value)){
			if(strstr($value,$filename)){
				$ISin=1;
				break;
			}
		}elseif(!$ISin){
			$ISin=ifin_array($array[$key],$filename,$ISin);
		}
	}
	return $ISin;
}
//显示所有子类别	 
 



//对比数组
function duibi($str,$str1){
	$a = explode(",",$str1);
	foreach ($a as $v) {	
		if ($v==$str){
			$cont='1';
			break; 
		}else{
			$cont="0";
		}
	}
	return $cont;
}

//还原转义字符传
function reduce($msg){
	$msg = str_replace("&lt;","<",$msg);
	$msg = str_replace("&gt;",">",$msg);
	$msg = str_replace('\&quot;','',$msg);
	$msg = str_replace('&quot;','"',$msg);
	$msg = str_replace('&#39;',"'",$msg);
	return $msg;
}


//把HTML代码过滤掉
function mk_html($str){
	$str=preg_replace('/<([^>]*)>/is',"",$str);	
	return $str;
}


//判断远程文件是否存在
function remote_file_exists($url_file){
//检测输入
	$url_file = trim($url_file);
	if (empty($url_file)) { 
		return false;
	}
	$url_arr = parse_url($url_file);
	if (!is_array($url_arr) || empty($url_arr)){ 
		return false;
	}
	
	//获取请求数据
	$host = $url_arr['host'];
	$path = $url_arr['path'] ."?". $url_arr['query'];
	$port = isset($url_arr['port']) ? $url_arr['port'] : "80";
	
	//连接服务器
	$fp = fsockopen($host, $port, $err_no, $err_str, 30);
	if (!$fp){
		 return false;
	}
	
	//构造请求协议
	$request_str = "GET ".$path." HTTP/1.1\r\n";
	$request_str .= "Host: ".$host."\r\n";
	$request_str .= "Connection: Close\r\n\r\n";
	
	//发送请求
	fwrite($fp, $request_str);
	$first_header = fgets($fp, 1024);
	fclose($fp);
	
	//判断文件是否存在
	if (trim($first_header) == ""){ 
		return false;
	}
	if (!preg_match("/200/", $first_header)){
		return false;
	}
	return true;
}


//存储照片在服务器
function base64_to_img( $base64_string, $output_file ) {
    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_string, $result)){
        $type = $result[2];
        $new_file = $output_file.".{$type}";
        if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_string)))){
            return $output_file.".{$type}";
        }
    }
    return false;
}

function create_guid() {
    $charid = strtoupper(md5(uniqid(mt_rand(), true)));
    $hyphen = chr(45);// "-"
    $uuid = chr(123)// "{"
        .substr($charid, 0, 8).$hyphen
        .substr($charid, 8, 4).$hyphen
        .substr($charid,12, 4).$hyphen
        .substr($charid,16, 4).$hyphen
        .substr($charid,20,12)
        .chr(125);// "}"
    return $uuid;
}



/* 数组转添加的sql 语句  */
function add_str($map){
    $di=0;
    $dwhe = "";
    foreach($map as $fled=>$val){
        if($fled!='id'){
            $di++;
            $dwhe.=($di>1?' , ':'')."`{$fled}`='{$val}'";
        }
    }
    return $dwhe;
}

/* 数组转查询的sql 语句 */
function select_str($arr){
    $di=0;
    $dwhe = "";
    foreach($arr as $fled=>$val){
        $di++;
        $dwhe .= ($di>1?' and  ':'')."`{$fled}`='{$val}'";
    }
    return $dwhe;
}

/**
 * 验证身份证号码
 *
 * @param string $vStr
 * @return boolean
 */
function verifyCard( $vStr ) {
    $vCity = array('11', '12', '13', '14', '15', '21', '22', '23', '31', '32', '33', '34', '35', '36', '37', '41', '42', '43', '44', '45', '46', '50', '51', '52', '53', '54', '61', '62', '63', '64', '65', '71', '81', '82', '91');
    if (!preg_match('/^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/', $vStr)) return false;
    if (!in_array(substr($vStr, 0, 2), $vCity)) return false;
    $vStr = preg_replace('/[xX]$/i', 'a', $vStr);
    $vLength = strlen($vStr);
    if ($vLength == 18) {
        $vBirthday = substr($vStr, 6, 4) . '-' . substr($vStr, 10, 2) . '-' . substr($vStr, 12, 2);
    } else {
        $vBirthday = '19' . substr($vStr, 6, 2) . '-' . substr($vStr, 8, 2) . '-' . substr($vStr, 10, 2);
    }
    if (date('Y-m-d', strtotime($vBirthday)) != $vBirthday) return false;
    if ($vLength == 18) {
        $vSum = 0;
        for ($i = 17; $i >= 0; $i--) {
            $vSubStr = substr($vStr, 17 - $i, 1);
            $vSum += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr, 11));
        }
        if ($vSum % 11 != 1)
            return false;
    }
    return true;
}

/* 验证电话 */
function checkPhone($phone)
{
    $check = '/^(1(([3456789][0-9])|(47)))\d{8}$/';
    if (preg_match($check, $phone)) {
        return true;
    } else {
        return false;
    }
}

// 根据身份证号 识别男女 1:女 2:男
function get_sex($idCard)
{
    $position = (strlen($idCard) == 15 ? -1 : -2);
    if (substr($idCard, $position, 1) % 2 == 0) {
        // return 1;
        return "女";
    }
    //return 2;
    return "男";
}
/**
 *  根据身份证号码计算年龄
 *  author:xiaochuan
 * @param string $idcard 身份证号码
 * @return int $age
 */
function get_age($idcard)
{
    if (empty($idcard)) return null;
    #  获得出生年月日的时间戳
    $date = strtotime(substr($idcard, 6, 8));
    #  获得今日的时间戳
    $today = strtotime('today');
    #  得到两个日期相差的大体年数
    $diff = floor(($today - $date) / 86400 / 365);
    #  strtotime加上这个年数后得到那日的时间戳后与今日的时间戳相比
    $age = strtotime(substr($idcard, 6, 8) . ' +' . $diff . 'years') > $today ? ($diff + 1) : $diff;
    return $age;
}